package com.sphd.beautysalon.activitys.marking;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.sphd.beautysalon.R;
import com.sphd.beautysalon.base.BaseActivity;
import com.sphd.beautysalon.base.RestaurantUrl;
import com.sphd.beautysalon.info.LoginInfo;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.presenter.MarketPresenterImpl;
import com.sphd.beautysalon.utils.Constant;
import com.sphd.beautysalon.view.MarketView;
import com.wzh.frame.utils.StringUtils;

import org.litepal.crud.DataSupport;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
* 营销
* */
public class MarketingActivity extends BaseActivity implements MarketView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.restaurant_foodtogo_layout)
    TextView restaurantFoodtogoLayout;
    @BindView(R.id.restaurant_coupon_layout)
    TextView restaurantCouponLayout;
    @BindView(R.id.restaurant_e_discount_layout)
    TextView restaurantEDiscountLayout;
    @BindView(R.id.text_chongzhi)
    TextView textChongzhi;
    @BindView(R.id.pay_switch)
    Switch paySwitch;
    @BindView(R.id.restaurant_pay_up_layout)
    RelativeLayout restaurantPayUpLayout;
    @BindView(R.id.zhekou_textview)
    TextView zhekouTextview;
    @BindView(R.id.restaurant_zhekou_layout)
    RelativeLayout restaurantZhekouLayout;
    @BindView(R.id.restaurant_jifen_layout)
    TextView restaurantJifenLayout;
    @BindView(R.id.content_marketing)
    NestedScrollView contentMarketing;
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    LoginInfo loginInfo;
    MarketPresenterImpl marketPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_marketing);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());

        paySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!buttonView.isPressed()) {
                    return;
                }
                if (isChecked) {
                    setIsCz(1);
                } else {
                    setIsCz(0);
                }
            }
        });
    }

    private void setIsCz(int i) {
        marketPresenter.setIsCzUrl(RestaurantUrl.Httptitle
                + RestaurantUrl.IsCz + "cgid=" + restaurantInfo.getId() + "&isCZ=" + i);
    }

    private void initView() {
        intent = getIntent();
        setTitleBar(toolbar, "营销管理");
        bundle = intent.getExtras();
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);

        Log.e("111111===",restaurantInfo.getStaff_status()+"");
        loginInfo = DataSupport.findLast(LoginInfo.class);
        marketPresenter = new MarketPresenterImpl(this);
        getResInfo();

//        recResInfo(restaurantInfo);
    }

    private void getResInfo() {
        marketPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.Restaurantmessage + "cgid=" + restaurantInfo.getId());
//        marketPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.RESTAURANT + "cgid=" + restaurantInfo.getId());

    }

    @OnClick({R.id.restaurant_foodtogo_layout, R.id.restaurant_coupon_layout,
            R.id.restaurant_e_discount_layout, R.id.restaurant_pay_up_layout,
            R.id.restaurant_zhekou_layout, R.id.restaurant_jifen_layout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.restaurant_foodtogo_layout:
                //送餐设置
                intent.setClass(context, FoodtogoSettingActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.restaurant_coupon_layout:
                //促销与优惠
                intent.setClass(context, CouponActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);

                break;
            case R.id.restaurant_e_discount_layout:
                //优惠券
                intent.setClass(context, EticketActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.restaurant_pay_up_layout:
                //充值
                if (paySwitch.isChecked()) {
                    intent.setClass(context, CzListActivity.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    toast("请先开启");
                }
                break;

            case R.id.restaurant_zhekou_layout:
                Log.e("===",restaurantInfo.getStaff_status()+"");
                Log.e("=22222222==",loginInfo.getStaff_auth()+"");
                if (isCg_has_storemanager(restaurantInfo)){
                    if (isManager(loginInfo)){
                        showZhekoudialog();
                    }else {
                        toast( "店长会去操作，您不用费心了");
                    }
                }else {
                    if (isSuperManager(loginInfo)){
                        showZhekoudialog();
                    }else {
                        toast( "店长会去操作，您不用费心了");
                    }
                }

//                if (isCanDo()) {
//                    toast( "店长会去操作，您不用费心了");
//                } else {
//                    showZhekoudialog();
//                }
                break;
            case R.id.restaurant_jifen_layout:
                intent.setClass(context, JifenActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);


                break;
        }
    }

    private void showZhekoudialog() {
        View view = getView(R.layout.layout_edittextdialog);
        AlertDialog alertDialog = setAlertDialog(view);
        alertDialog.setTitle("设置商品的折扣");
        final EditText userInput = (EditText) view
                .findViewById(R.id.input);
        userInput.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
        userInput.setHint("大于0小于等于10的折扣率");
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                String input=userInput.getText().toString().trim();

                if (StringUtils.isEmpty(input)){
                    toast("折扣率不能为空");
                }else {
                    if (10>=Double.parseDouble(input)&&Double.parseDouble(input)>0){
                        marketPresenter.setDisUrl(RestaurantUrl.Httptitle
                                + RestaurantUrl.AddZhekou+"discount="+input+"&cgid="+restaurantInfo.getId());
                    }else {
                        toast("请输入大于0小于等于10的商品折扣率");
                    }
                }



            }
        });
        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        alertDialog.show();

    }

    private boolean isCanDo() {
        return (isCg_has_storemanager(restaurantInfo) && isSuperManager(loginInfo));
    }

    @Override
    public void recResInfo(RestaurantInfo restaurantInfo) {
        if (restaurantInfo != null) {
            this.restaurantInfo = restaurantInfo;
        } else {
            toast("获取餐馆信息失败");
        }
        paySwitch.setChecked(restaurantInfo.isCg_iscz());
        zhekouTextview.setText("会员折扣:" + turn(restaurantInfo.getCg_discount()) + "折");
    }

    @Override
    public void recRes(int res) {
        if (res == 1) {
            toast("设置成功");
        } else {
            toast("设置失败");
        }
        getResInfo();
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
